package cn.ym.product.service.impl;

import cn.ym.basic.service.IBaiduAiAuditService;
import cn.ym.basic.util.AjaxResult;
import cn.ym.basic.util.LoginContext;
import cn.ym.org.domain.Employee;
import cn.ym.org.mapper.EmployeeMapper;

import cn.ym.product.domain.Product;
import cn.ym.product.domain.ProductDetail;
import cn.ym.product.domain.ProductOnlineAuditLog;

import cn.ym.product.mapper.ProductDetailMapper;
import cn.ym.product.mapper.ProductMapper;
import cn.ym.product.mapper.ProductOnlineAuditLogMapper;
import cn.ym.product.service.IProductService;
import cn.ym.basic.service.impl.BaseServiceImpl;
import cn.ym.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {


    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;
    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;

    /**
     *
     * 上架：业务类
     * @param ids
     * @param request
     */
    @Override
    public AjaxResult onsale(Long[] ids, HttpServletRequest request) {
        //从请求头获取logininfo对象
        Logininfo logininfo = LoginContext.getLogininfo(request);
        //通过LogininfoId查询到employee对象
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        for (Long id : ids) {
            //通过id查询到product对象
            Product product = productMapper.findOne(id);
            //1.审核 - 智能审核
            Boolean boo1 = baiduAiAuditService.textAudit(product.getName());
            Boolean boo2 = baiduAiAuditService.imageAudit(product.getResources());
            //审核通过
            if (boo1 && boo2){
                //2.审核通过：修改状态state=1，上架时间 - 添加审核日志
                productMapper.updateOnSaleState(id,new Date());
                //添加审核日志对象
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                log.setProductId(id);
                log.setState(1);
                log.setNote("审核通过");
                log.setAuditId(employee.getId());

                //添加到审核日志
                productOnlineAuditLogMapper.add(log);
            }else {
                //3.审核失败：添加审核日志
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                log.setProductId(id);
                log.setState(0);
                log.setNote("审核失败");
                log.setAuditId(employee.getId());
                //添加到审核日志
                productOnlineAuditLogMapper.add(log);

                return AjaxResult.me().setSuccess(false).setMsg("上架失败");
            }
        }
        return new AjaxResult();

    }

    /**
     * 下架：业务类
     * @param ids
     * @param
     */
    @Override
    public void offsale(Long[] ids) {
        //1.修改状态，修改下架的时间

        productMapper.updateOffState(ids,new Date());
    }

//---------- 增删改的重构 ------------------------------
    /**
     * 添加
     * @param product
     */
    @Override
    public void add(Product product) {
        super.add(product);
        //获取服务详情
        ProductDetail productDetail = product.getProductDetail();
        //判断服务不为空
        if(productDetail!=null){
            //设置服务的ProductId
            productDetail.setProductId(product.getId());
            //添加服务详情
            productDetailMapper.add(productDetail);
        }
    }
    /**
     * 修改
     * @param product
     */
    @Override
    public void update(Product product) {
        super.update(product);
        //获取服务详情
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail != null){
            //之前在分页查询已经将id查询出来了 - 前端row中有
            productDetailMapper.updateProductDetailByProductId(product);
        }
    }
    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Long id) {
        //删除服务详情信息 - 根据服务id【注意不能掉用del】
        productDetailMapper.deleteProductDetailByProductId(id);
        //删除服务信息
        super.delete(id);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void patchDelete(List<Long> ids) {
        for (Long id : ids) {
            delete(id);
        }
    }


}
